var userId = localStorage.getItem("userid");
var publicImg;
var newPro = $("#problem_text");
var sort;
$(function(){
	getPic();
	savePro(newPro);
	$("#identyCardPathInput").click(function(){
		publicImg=$("#picPath");
		openSelectImgDiv();
	});
	$('#problem_text').tap(function() {
		newPro = $("#problem_text");
			$("#question").val('');
		savePro(newPro);
	});
	$('#newQust').tap(function() {
		newPro = $("#newQust");
		$("#question").val('');
		savePro(newPro);
	});
	$('#hold').click(function(){
		savePro(newPro);
		if(newPro.attr('id') == "problem_text"){
			sort = 1;
			problemAdd(sort);
		}
		if(newPro.attr('id') == "newQust"){
			sort = 2;
			problemAdd(sort);
		}
	
	});
	$("#subKey").tap(function(){
		queding();
	});
});


function savePro(pro){
	if(pro.attr('id') =="problem_text"){
	  $('#question').attr('placeholder','请填写您遇到的问题（140个字）');
	}
	if(pro.attr('id') == 'newQust'){
		$('#question').attr('placeholder','请填写您的新品需求（140个字）');
	}
	
}
function getPic(){
	$.ajax({
		type : 'POST',
		async : false,
		data : {userId:userId},
		url : server_url+'rest/frontUser/getPic.do?ran='+Math.random(),
		success : function(data) {
			if (data.retCode == "0000") {
				var pasteData = data.data;
				$("#compName").text(pasteData.buyerInfoVo.compName);
				if(pasteData.picPath != null && pasteData.picPath !=""){
				$("#picPath").attr("src",pasteData.picPath);
				$("#picPath").val(pasteData.picPath);
				}else{
					$("#picPath").attr("src","/LianniuWeb/img/buyer/user.png");
				}
			} else{
				alert("操作失败");
			}
		}
	});
}
function picImagEdit(data,input){
	$.ajax({
		type:"POST",
		async : false,
		data:{identyCardPath:data,userId:userId},
		url : server_url+'rest/frontUser/updatePic.do?ran='+Math.random(),
		success:function(data){
			if(data.retCode == "0000"){
				var path = data.data;
				$("input[name=picPath]").val(path);
				input.attr("src",path);
				cancelSelectImgDiv();
				/*$("#"+id).siblings("input[type=hidden]").val(path);
				$("#"+id).siblings("img").attr("src",path);*/
			}else{
			alert("网络异常,请稍后再试");
			}
		},
	});
}
function problemAdd(sort){
	var question=$("#question").val();
	if(empty(question)){
		alert("内容不能为空!");
		return;
	}
	if(question.length > 140){
		alert("内容不能超过140个字!");
		return;
	}
	if(!checkQuest(question)){
		alert("内容输入不规范!");
		return;
	}
	if(question !=""){
		$("#problem").css({"display":"none"});
	}
	 $.ajax({
         type : "POST",
         dataType : "json",
         url :server_url+'rest/suggest/suggestAdd.do?ran='+Math.random(),
         data : {question:question,sort:sort},
         contentType : "application/x-www-form-urlencoded; charset=utf-8",
         success : function(data) {
        	 //console.log(JSON.stringify(data));
        	 if(data.retCode == "0000"){
        		alert("提交成功");
        	 }
         }
         });
}
/*function newQustAdd(){
	var question=$("#problem").val();
	 $.ajax({
         type : "POST",
         dataType : "json",
         url :server_url+'rest/suggest/suggestAdd.do?ran='+Math.random(),
         data : {question:question},
         contentType : "application/x-www-form-urlencoded; charset=utf-8",
         success : function(data) {
        	 if(data.retCode == "0000"){
        		 $("#problem").val(""); 
        	 }
         }
         });
}*/

function checkQuest(question){
	var reg=/([\u4E00-\u9FA5]|[\uFE30-\uFFA0]|[0-9])+/;
	 return reg.test(question);
}
function queding(){
	if(checkPrivateKey()){
		supplyAdd();
	}
}

function supplyAdd(){
	$.ajax({
        type : "POST",
        dataType : "json",
        url :server_url+'rest/supply/addSupply.do?ran='+Math.random(),
        data : {userId:userId},
        success : function(data) {
        	if(data.retCode == "0000"){
       		 	localStorage.setItem("toSupplyId",data.data);
       		 	guan();
       		 checkKeyRemote(data.data,$("#privateKey").val(),goIndex);
       	 	}else{
       	 		alert("转换卖家失败!")
       	 	}
        }
    });
	
}
var checkSupplyKey = function(){
	$('.moskImg1').css('display','block');
	$.ajax({
        type : "POST",
        dataType : "json",
        url :server_url+'rest/buyer/checkSupplyKey.do?ran='+Math.random(),
        data : {userId:userid},
        success : function(data) {
        	$('.moskImg1').css('display','none');
        	if(data.retCode == "0000"){
        		goIndex();
       	 	}else{
       	 		invCode();
       	 	}
        },
        error:function(){
        	alert('系统异常');
        	$('.moskImg1').css('display','none');
        }
});
}
function goIndex(){
	window.location.href=server_url+"index.do";
}
function setUp(){
	window.location=server_url+"setUp.do";
}
var toCall = function(){
	window.location.href="tel:010-57019707";
}
function updateAddr(){
	window.location.href=server_url+"buyer/product/editConsigneeAddr.do";	
}

/*上传图片开始*/
//取消
function cancelSelectImgDiv(){
	
	$(".moskImg").css("display","none");
	$(".selecImg").css({
		"display":"block",
		bottom:'-2.92rem',
		animation:'0.5s move1',
	});
	setTimeout(function(){$("#selecImg").css({
		display:'none'
	})},500);
}
//打开选择窗
function openSelectImgDiv(){
	$(".moskImg").css({
		display:'block'
	});
	$("#selecImg").css({
		display:"block",
		bottom:'0rem',
		animation:'0.5s move'
	});
}
//调用摄像头
function takePhoto(){
	window.photo.openCamera();
}

//调用相册
function chooseAlbum(){
	window.photo.openCamera1();
}

//获取返回路径
function getPicture(path){
	
	var s = replaceAll(path,'//','/');
	s = replaceAll(s,'"','');
	//s就是处理过的返回路径
	
	picImagEdit(s,publicImg);
	
}
//获取返回路径
function getPicture1(path){
	var s = replaceAll(path,'//','/');
	s = replaceAll(s,'"','');
	picImagEdit(s,publicImg);
}
//字符串替换
function replaceAll(s,s1,s2){
	while(s.indexOf(s1)>=0){
		s = s.replace(s1,s2);
	}
	return s;
}

